package pl.polsl.pum2.pj.math;

public class Vector3d {
  public float x;
  public float y;
  public float z;

  private Vector3d() {}

  public Vector3d(float x, float y, float z) {
    this.x = x;
    this.y = y;
    this.z = z;
  }

  public float[] toFloatArray3() {
    return new float[] {x, y, z};
  }

  public float[] toFloatArray4() {
    return new float[] {x, y, z, 1f};
  }

  public void updateFromArray(float[] tab) {
    x = tab[0];
    y = tab[1];
    z = tab[2];
  }

  public static Vector3d empty() {
    return new Vector3d();
  }

  public static float magnitude(Vector3d vector) {
    return (float) Math.sqrt((vector.x * vector.x) + (vector.y * vector.y) + (vector.z * vector.z));
  }

  public static void normalize(Vector3d vector) {
    float vecMag = magnitude(vector);
    if (vecMag == 0.0f) {
      vector.x = 1.0f;
      vector.y = 0.0f;
      vector.z = 0.0f;
      return;
    }
    vector.x /= vecMag;
    vector.y /= vecMag;
    vector.z /= vecMag;
  }

  public static float dotProduct(Vector3d vector1, Vector3d vector2) {
    return vector1.x * vector2.x + vector1.y * vector2.y + vector1.z * vector2.z;
  }

  public static Vector3d crossProduct(Vector3d vector1, Vector3d vector2) {
    Vector3d ret = new Vector3d();
    ret.x = (vector1.y * vector2.z) - (vector1.z * vector2.y);
    ret.y = (vector1.z * vector2.x) - (vector1.x * vector2.z);
    ret.z = (vector1.x * vector2.y) - (vector1.y * vector2.x);
    return ret;
  }

  public Vector3d clone() {
    return new Vector3d(x, y, z);
  }

  public static float distance(Vector3d a, Vector3d b) {
    float dx = a.x - b.x;
    float dy = a.y - b.y;
    float dz = a.z - b.z;
    return (float) Math.sqrt(dx * dx + dy * dy + dz * dz);
  }

}
